$(function(){
	
	$( "#tabs" ).tabs();	
	//initiate dataTables plugin
	var myTable = 
	$('#dynamic-table')
	//.wrap("<div class='dataTables_borderWrap' />")   //if you are applying horizontal scrolling (sScrollX)
	.DataTable( {
		bAutoWidth: false,		
		"order":[[5,"desc"]],//默认排序
		"columnDefs":[
		    	 	{"orderable":false,"targets":[0,0]},
		    	 	{"orderable":false,"targets":[0,2]},
		    	 	{"orderable":false,"targets":[0,3]},
					{"orderable":false,"targets":[0,6]},

		 ],
		"aaSorting": [],
		
		
		"bProcessing": true,
        "bServerSide": true,
      	"ajax": {
            "url": "index.php?r=membermanage/data",								
            "type": "POST"
        },
        "columns": [
            { "data": "check" },
            { "data": "username" },
            { "data": "Email" },
            { "data": "lName" },
            { "data": "Balance" },
            { "data": "RegDate" },
			{ "data": "options" }
        ],
        //语言汉化
        language: {
        	'url':'aceAsset/i18n/zh_cn.json',
        },
		       
		select: {
			style: 'multi'
		}
    } );

	
	
	$.fn.dataTable.Buttons.swfPath = "aceAsset/js/dataTables/extensions/buttons/swf/flashExport.swf"; //in Ace demo aceassets/assets will be replaced by correct assets path
	$.fn.dataTable.Buttons.defaults.dom.container.className = 'dt-buttons btn-overlap btn-group btn-overlap';
	
	new $.fn.dataTable.Buttons( myTable, {
		buttons: [
		  {
			"extend": "colvis",
			"text": "<i class='fa fa-search bigger-110 blue'></i> <span class='hidden'>Show/hide columns</span>",
			"className": "btn btn-white btn-primary btn-bold",
			columns: ':not(:first):not(:last)'
		  },
		  {
			"extend": "copy",
			"text": "<i class='fa fa-copy bigger-110 pink'></i> <span class='hidden'>Copy to clipboard</span>",
			"className": "btn btn-white btn-primary btn-bold"
		  },
		  {
			"extend": "csv",
			"text": "<i class='fa fa-database bigger-110 orange'></i> <span class='hidden'>Export to CSV</span>",
			"className": "btn btn-white btn-primary btn-bold"
		  },
		  {
			"extend": "excel",
			"text": "<i class='fa fa-file-excel-o bigger-110 green'></i> <span class='hidden'>Export to Excel</span>",
			"className": "btn btn-white btn-primary btn-bold"
		  },
		  {
			"extend": "pdf",
			"text": "<i class='fa fa-file-pdf-o bigger-110 red'></i> <span class='hidden'>Export to PDF</span>",
			"className": "btn btn-white btn-primary btn-bold"
		  },
		  {
			"extend": "print",
			"text": "<i class='fa fa-print bigger-110 grey'></i> <span class='hidden'>Print</span>",
			"className": "btn btn-white btn-primary btn-bold",
			autoPrint: false,
			message: 'This print was produced using the Print button for DataTables'
		  }		  
		]
	} );
	myTable.buttons().container().appendTo( $('.tableTools-container') );
	
	//style the message box
	var defaultCopyAction = myTable.button(1).action();
	myTable.button(1).action(function (e, dt, button, config) {
		defaultCopyAction(e, dt, button, config);
		$('.dt-button-info').addClass('gritter-item-wrapper gritter-info gritter-center white');
	});
	
	
	var defaultColvisAction = myTable.button(0).action();
	myTable.button(0).action(function (e, dt, button, config) {
		
		defaultColvisAction(e, dt, button, config);
		
		
		if($('.dt-button-collection > .dropdown-menu').length == 0) {
			$('.dt-button-collection')
			.wrapInner('<ul class="dropdown-menu dropdown-light dropdown-caret dropdown-caret" />')
			.find('a').attr('href', '#').wrap("<li />")
		}
		$('.dt-button-collection').appendTo('.tableTools-container .dt-buttons')
	});

	////

	setTimeout(function() {
		$($('.tableTools-container')).find('a.dt-button').each(function() {
			var div = $(this).find(' > div').first();
			if(div.length == 1) div.tooltip({container: 'body', title: div.parent().text()});
			else $(this).tooltip({container: 'body', title: $(this).text()});
		});
	}, 500);
	
	myTable.on( 'select', function ( e, dt, type, index ) {
		if ( type === 'row' ) {
			$( myTable.row( index ).node() ).find('input:checkbox').prop('checked', true);
		}
	} );
	myTable.on( 'deselect', function ( e, dt, type, index ) {
		if ( type === 'row' ) {
			$( myTable.row( index ).node() ).find('input:checkbox').prop('checked', false);
		}
	} );
			
	/////////////////////////////////
	//table checkboxes
	$('th input[type=checkbox], td input[type=checkbox]').prop('checked', false);
	
	//select/deselect all rows according to table header checkbox
	$('#dynamic-table > thead > tr > th input[type=checkbox], #dynamic-table_wrapper input[type=checkbox]').eq(0).on('click', function(){
		var th_checked = this.checked;//checkbox inside "TH" table header
		
		$('#dynamic-table').find('tbody > tr').each(function(){
			var row = this;
			if(th_checked) myTable.row(row).select();
			else  myTable.row(row).deselect();
		});
	});
	
	//select/deselect a row when the checkbox is checked/unchecked
	$('#dynamic-table').on('click', 'td input[type=checkbox]' , function(){
		var row = $(this).closest('tr').get(0);
		if(!this.checked) myTable.row(row).deselect();
		else myTable.row(row).select();
	});



	$(document).on('click', '#dynamic-table .dropdown-toggle', function(e) {
		e.stopImmediatePropagation();
		e.stopPropagation();
		e.preventDefault();
	});
		//add tooltip for small view action buttons in dropdown menu
	$('[data-rel="tooltip"]').tooltip({placement: tooltip_placement});
	
	//tooltip placement on right or left
	function tooltip_placement(context, source) {
		var source = $(source);
		var parent = source.closest('table')
		var off1 = parent.offset();
		var w1 = parent.width();

		var off2 = source.offset();
		//var w2 = source.width();

		if( parseInt(off2.left) < parseInt(off1.left) + parseInt(w1 / 2) ) return 'right';
		return 'left';
	}
	
});


//查看会员的收货信息
function see(id){
	$.widget("ui.dialog", $.extend({}, $.ui.dialog.prototype, {
		_title: function(title) {
			var $title = this.options.title || '&nbsp;'
			if( ("title_html" in this.options) && this.options.title_html == true )
				title.html($title);
			else
				 title.text($title);
		}
	}));
	var id=id;
	$.ajax({
		type:"post",
		url:"index.php?r=membermanage/mempreview",
		data:{	
				"action":"query_action",
				"id":id,				
			},
		dataType:"html",
		success:function(msg){
			$("#dialog-message").html(msg);
		}
	});
	var dialog = $( "#dialog-message" ).removeClass('hide').dialog({
		modal: true,
		width:500,
		title: "<div class='widget-header widget-header-small'><h4 class='smaller'><i class='ace-icon fa fa-check'></i> 会员收货信息</h4></div>",
		title_html: true,
		
	});
}
//修改会员的信息
function update(id){
	$.widget("ui.dialog", $.extend({}, $.ui.dialog.prototype, {
		_title: function(title) {
			var $title = this.options.title || '&nbsp;'
			if( ("title_html" in this.options) && this.options.title_html == true )
				title.html($title);
			else title.text($title);
		}
	}));
	var id=id;
	$.ajax({
		type:"post",
		url:"index.php?r=membermanage/memupdate",
		data:{
			"action":"update_action",
			"id":id		
		},
		dataType:"html",
		success:function(msg){
			$("#dialog-message").html(msg);
		}
	});
	var dialog = $( "#dialog-message" ).removeClass('hide').dialog({
		modal: true,
		width:500,
		title: "<div class='widget-header widget-header-small'><h4 class='smaller'><i class='ace-icon fa fa-check'></i> 会员信息修改</h4></div>",
		title_html: true,
		
	});
}
//删除会员的信息

function show_confirm(id){
	var id=id;
	bootbox.setLocale("zh_CN");
	bootbox.confirm("是否确认删除?", function(result) {
		
		if(result==true) {
			$.ajax({
				type:"post",
				url:"index.php?r=membermanage/delete",
				data:{
					"action":"delete",
					"id":id
				},
				success:function(msg){
					
					alert(msg);
			},
			});
		}
	});
}
